<template>
	<view>
		<view class="top">
			<image class="bjt" src="../../../static/images/ljh/bjt.png"></image>
			<view class="title">登录</view>
			<view class="nh">
				您好，
			</view>
			<view class="hyy">
				欢迎登录车联网服务！
			</view>
		</view>
		<view class="cl">
			<image src="../../../static/images/ljh/cl.png"></image>
		</view>
		<view class="container" style="margin-top: 40rpx;">
			<!-- 手机号登录 -->
			<view class="input-group">
				<!-- <text class="label">手机号登录</text> -->
				<input class="input" v-model="loginForm.username" type="text" placeholder="账号" />
			</view>

			<view class="input-group">
				<!-- <text class="label">手机号登录</text> -->
				<input class="input" v-model="loginForm.password" type="text" placeholder="密码" />
			</view>

			<!-- 输入验证码 -->
			<view class="input-group">
				<input class="input" v-model="loginForm.code" type="text" placeholder="输入验证码" />
				<!-- <button class="code-button" @click="getImg()">获取验证码</button> -->
				<img :src="codeUrl" @click="getCode" style="width: 200rpx; height: 60rpx;" />
			</view>

			<!-- 登录按钮 -->
			<button class="login-button" @click="login_btn">登录</button>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				loginForm: {
					code: '',
					password: "admin123",
					username: "zhangsan",
					uuid: ""
				},
				codeUrl: ''
			}
		},
		onLoad() {
			this.getCode();
		},
		methods: {
			// 获取通知公告
			getNotice() {
				this.$http({
					url: '/api/system/notice/list',
					method: 'GET',
					success(res) {
						//console.log(res)
					}
				})
			},
			// 获取验证码函数
			getCode() {
				// 直接从原型链中获取封装好的request对象即可
				let that = this;
				this.$http({
					url: '/api/code',
					method: 'GET',
					success(res) {
						that.codeUrl = "data:image/gif;base64," + res.data.img;
						// console.log(that.codeUrl)
						// console.log(res.data.uuid)
						that.loginForm.uuid = res.data.uuid;
					}
				})
			},
			// 登录
			login_btn() {
				let that = this;
				// 接受数据，发送请求即可
				this.$http({
					url: 'api/user/userLogin/login',
					data: {
						username: that.loginForm.username,
						password: that.loginForm.password,
					},
					method: 'post',
					success(res) {
						console.log(res)
						if (res.data.data != null) {
							uni.setStorageSync("user_id", res.data.data.id)
							let form = {
								code: that.loginForm.code,
								password: "admin123",
								username: "admin",
								uuid: that.loginForm.uuid
							}
							that.$http({
								url: '/api/auth/login',
								method: 'POST',
								data: form,
								success(res) {
									console.log(res)
									// 如果登录成功，则需要把token保存到本地
									if (res.data.code == 200) {
										// 缓存数据
										uni.setStorageSync("token", res.data.data.access_token);

										// console.log(res.data.data.access_token)
										//跳转页面
										alert("登录成功");
										that.getNotice();
										uni.$u.route({
											url: '/pages/index/index',
											type: 'tab'
										});
									} else {
										alert('登录失败！')
									}
								}
							})
						}else{
							alert("账号或密码错误！");
						}
					}
				})

			}
		}
	}
</script>
<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20px;
		background-color: #ffffff;
		height: 100vh;
		box-sizing: border-box;
	}

	.input-group {
		display: flex;
		align-items: center;
		margin-bottom: 15px;
		width: 100%;
		border-bottom: 1px solid #e0e0e0;
	}

	.label {
		color: #999999;
		font-size: 14px;
		flex-shrink: 0;
		margin-right: 10px;
	}

	.input {
		flex: 1;
		font-size: 16px;
		color: #333333;
		border: none;
		outline: none;
	}

	.code-button {
		background-color: #0084ff;
		color: #ffffff;
		border: none;
		border-radius: 5px;
		padding: 5px 10px;
		font-size: 14px;
		cursor: pointer;
	}

	.login-button {
		background: linear-gradient(90deg, #007bff, #3399ff);
		color: white;
		border: none;
		border-radius: 25px;
		padding: 7px;
		font-size: 16px;
		text-align: center;
		margin-top: 20px;
		width: 80%;
		height: 100rpx;
		align-items: center;
	}

	.cl {
		position: absolute;
		top: 300rpx;
		left: 400rpx;
	}

	.cl image {
		width: 300rpx;
		height: 200rpx;
	}

	.hyy {
		font-size: 32rpx;
		color: white;
		position: absolute;
		top: 220rpx;
		left: 120rpx;
	}

	.nh {
		position: absolute;
		top: 170rpx;
		left: 120rpx;
		font-size: 44rpx;
		color: white;
	}


	.bjt {
		width: 100%;
		height: 450rpx;
	}

	.top {
		width: 100%;
		height: 450rpx;
		/* background-image: url(@/static/images/ljh/bjt.png); */
		border-radius: 0 0rpx 40rpx 40rpx;
	}

	.title {
		width: 100%;
		color: white;
		text-align: center;
		padding: 50rpx 0rpx;
		font-size: 35rpx;
	}
</style>